﻿using DevExpress.XtraEditors;
using ImageDefine;
using SmartDisplayControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TableModule;

namespace CustomModule
{
    public partial class FormFirstTimeToolSetting : DevExpress.XtraEditors.XtraForm
    {
        public FormFirstTimeToolSetting()
        {
            InitializeComponent();
        }

        private void FormFirstTimeToolSetting_Load(object sender, EventArgs e)
        {
            TableModule.FormDebugSimple formDebugSimple = new TableModule.FormDebugSimple();
            formDebugSimple.TopLevel = false;
            formDebugSimple.Size = panelControl1.Size;
            panelControl1.Controls.Add(formDebugSimple);
            formDebugSimple.Show();
            propertyGridControl1.SelectedObject = FrameManager.FrameManager.ProductSetting;

            if (FrameManager.MachineStatus.Is4Axis == false)
            {
                labelControl2.Visible = false;
                labelControl3.Visible = false;
                labelControl4.Visible = false;
                simpleButton设定第二个点.Visible = false;
                simpleButton计算工具.Visible = false;
                simpleButton验证工具.Visible = false;
                labelControl1.Visible =false;
                simpleButton设定第一个点.Text = "设定基准点";
            }
            
        }

        private void simpleButton设定第一个点_Click(object sender, EventArgs e)
        {
            TableRunner table = TableManager.tableRunner;
            if (FrameManager.MachineStatus.Is4Axis)
            {
                if (TableManager.tableActioner.tableDateGlabal.dicTableDataPos.Keys.Contains("对针位置1"))
                {
                    TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                    TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置1"].dXPos = tableDataPosition.dXPos;
                    TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置1"].dYPos = tableDataPosition.dYPos;
                    TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置1"].dZPos = tableDataPosition.dZPos;
                    TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置1"].dUPos = tableDataPosition.dUPos;
                }
            }
            else
            {
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dX = 0.0;
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dY = 0.0;
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dXPos = tableDataPosition.dXPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dYPos = tableDataPosition.dYPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dZPos = tableDataPosition.dZPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dUPos = tableDataPosition.dUPos;
                TableManager.tableRunner.SetTool("Default");
                TableManager.Save();
            }
           
           
        }

        private void simpleButton设定第二个点_Click(object sender, EventArgs e)
        {
            TableRunner table = TableManager.tableRunner;
            if (TableManager.tableActioner.tableDateGlabal.dicTableDataPos.Keys.Contains("对针位置2"))
            {
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置2"].dXPos = tableDataPosition.dXPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置2"].dYPos = tableDataPosition.dYPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置2"].dZPos = tableDataPosition.dZPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["对针位置2"].dUPos = tableDataPosition.dUPos;
            }
        }

        private void simpleButton计算工具_Click(object sender, EventArgs e)
        {
            CreateTool();
        }
        public void CreateTool()
        {
            try
            {
                TableDataPosition tablePos1 = TableManager.tableRunner.GetPosition("对针位置1");
                TableDataPosition tablePos2 = TableManager.tableRunner.GetPosition("对针位置2");
                //距离平方
                double dRTemp1 = (tablePos1.dXPos - tablePos2.dXPos) * (tablePos1.dXPos - tablePos2.dXPos) + (tablePos1.dYPos - tablePos2.dYPos) * (tablePos1.dYPos - tablePos2.dYPos);
                //距离的一半
                double dRTemp2 = Math.Sqrt(dRTemp1) / 2.0;
                //半径
                double dAngleDiffte1 = 90.0 - Math.Abs((tablePos2.dUPos - tablePos1.dUPos) / 2.0);
                double dR = dRTemp2 / Math.Cos((dAngleDiffte1 / 180.0) * Math.PI);
                //角度
                //double dAngleTemp1 = Math.Atan2(tablePos2.dYPos - tablePos1.dYPos, tablePos2.dXPos - tablePos1.dXPos);
                double dAngleTemp1 = Math.Atan2(tablePos2.dYPos - tablePos1.dYPos, tablePos2.dXPos - tablePos1.dXPos);
                double dDiffAngle = tablePos2.dUPos - tablePos1.dUPos;
                double dDiffAngleTemp;
                if (dDiffAngle > 0)
                {
                    dDiffAngleTemp = 90 - dDiffAngle / 2.0;
                }
                else
                {
                    dDiffAngleTemp = -(90 - Math.Abs(dDiffAngle / 2.0));
                }
                double dAngle = (dAngleTemp1 / Math.PI) * 180.0 + dDiffAngleTemp - tablePos1.dUPos;

                double dX = dR * Math.Cos((dAngle / 180.0) * Math.PI);
                double dY = dR * Math.Sin((dAngle / 180.0) * Math.PI);
                //TableManager.tableRunner.UpdateTool("Nozzle", dX, dY);
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dX = dX;
                TableManager.tableActioner.tableDateGlabal.dicTableDataTool["Nozzle"].dY = dY;
                TableManager.tableRunner.SetTool("Nozzle");
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereAtToolAndLocal();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dXPos = tableDataPosition.dXPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dYPos = tableDataPosition.dYPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dZPos = tableDataPosition.dZPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["针头工具位置"].dUPos = tableDataPosition.dUPos;
                TableManager.tableRunner.SetTool("Default");
                TableManager.Save();

            }
            catch
            {

            }
        }

        private void simpleButton验证工具_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() => 
            {
                ToolConfirm();
            });
        }
        public void ToolConfirm()
        {
            TableManager.tableRunner.SetTool("Nozzle");
            TableManager.tableRunner.Jump("针头工具位置");
            System.Threading.Thread.Sleep(2000);
            TableDataPosition position = TableManager.tableRunner.GetPosition("针头工具位置");
            position.dUPos = position.dUPos - 30;
            TableManager.tableRunner.Jump(position);
            //System.Threading.Thread.Sleep(1000);
            TableManager.tableRunner.SetTool("Default");
            

            XtraMessageBox.Show("确定针头是否和基准对准");
        }

        private void simpleButton安全位置设定_Click(object sender, EventArgs e)
        {
            TableRunner table = TableManager.tableRunner;
            if (TableManager.tableActioner.tableDateGlabal.dicTableDataPos.Keys.Contains("SafePos"))
            {
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["SafePos"].dXPos = tableDataPosition.dXPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["SafePos"].dYPos = tableDataPosition.dYPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["SafePos"].dZPos = tableDataPosition.dZPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["SafePos"].dUPos = tableDataPosition.dUPos;
                double d追踪区间大小 = FrameManager.FrameManager.ProductSetting.追踪区间大小;
                if (FrameManager.FrameManager.ProductSetting.流水线反反方向)
                {
                    TableManager.tableActioner.tableDate.dicTableDataPos["MinPos"].dXPos = tableDataPosition.dXPos - d追踪区间大小;
                    TableManager.tableActioner.tableDate.dicTableDataPos["MaxPos"].dXPos = tableDataPosition.dXPos + 10;
                }
                else
                {
                    TableManager.tableActioner.tableDate.dicTableDataPos["MinPos"].dXPos = tableDataPosition.dXPos - 10;
                    TableManager.tableActioner.tableDate.dicTableDataPos["MaxPos"].dXPos = tableDataPosition.dXPos + d追踪区间大小;
                }
                
                
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            TableRunner table = TableManager.tableRunner;
            if (TableManager.tableActioner.tableDateGlabal.dicTableDataPos.Keys.Contains("排胶位置"))
            {
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["排胶位置"].dXPos = tableDataPosition.dXPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["排胶位置"].dYPos = tableDataPosition.dYPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["排胶位置"].dZPos = tableDataPosition.dZPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["排胶位置"].dUPos = tableDataPosition.dUPos;
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            TableRunner table = TableManager.tableRunner;
            if (TableManager.tableActioner.tableDateGlabal.dicTableDataPos.Keys.Contains("称重位置"))
            {
                TableDataPosition tableDataPosition = TableManager.tableActioner.WhereTable();
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["称重位置"].dXPos = tableDataPosition.dXPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["称重位置"].dYPos = tableDataPosition.dYPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["称重位置"].dZPos = tableDataPosition.dZPos;
                TableManager.tableActioner.tableDateGlabal.dicTableDataPos["称重位置"].dUPos = tableDataPosition.dUPos;
            }
        }

        private void FormFirstTimeToolSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (Control control in Controls)
            {
                Form form = control as Form;
                if (form != null)
                {
                    form.Close();
                }
            }
        }

        private void simpleButton移动到安全位置_Click(object sender, EventArgs e)
        {
            TableManager.tableRunner.SetTool("Default");
            TableDataPosition position = TableManager.tableRunner.GetPosition("SafePos");
            //position.dUPos = position.dUPos - 30;
            TableManager.tableRunner.Jump(position,false);
            
            
        }
    }
}